package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.BrandDao;
import com.buss.product.model.Brand;

@Repository
public class BrandDaoImpl implements BrandDao {

	@Autowired
	private BaseDao<Brand> baseDao;
	
	@Override
	public Brand saveBrand(Brand brand) {
		return baseDao.saveOrEdit(brand);
	}

	@Override
	public void editBrand(Brand brand) {
		baseDao.edit(brand);
	}

	@Override
	public void removeBrand(Long[] ids) {
		String sql = "delete from Brand brand where brand.id in (:ids)";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		baseDao.excuteHsql(sql, map);
	}

	@Override
	public Brand loadBrand(Long id) {
		return baseDao.get(id, Brand.class);
	}
	
	@Override
	public ResultList<Brand> queryBrand(String companyId, String name,String pinyin,
			Long[] categoryId, OrderBy orderBy, Paginalbe page) {
		String sql = "select distinct brand from Brand brand ";
		if(categoryId != null && categoryId.length > 0){
			sql += " inner join brand.categorys category ";
		}
		sql += " where brand.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and brand.name like :name ";
			params.put("name", "%" +name+ "%");
		}
		if(StringUtil.isNotEmpty(pinyin)){
			sql += " and brand.pinyin like :pinyin ";
			params.put("pinyin", "%" + pinyin + "%");
		}
		if(categoryId != null && categoryId.length > 0){
			sql += " and category.id in (:categoryId) ";
			params.put("categoryId", categoryId);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("sortOrder", "desc");
		}
		sql += orderBy.aggOrder("brand");
		
		return baseDao.queryHsql(sql, params, page);
	}

	@Override
	public ResultList<Brand> queryBrand(Long categoryId) {
		String sql = "select brand from Brand brand inner join brand.categorys category " +
				"where category.id = :categoryId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		return baseDao.queryHsql(sql, params, null);
	}

	@Override
	public ResultList<Brand> queryBrandNotInCategoryId(Long categoryId) {
		String sql = "select * from base_brand where id not in (select brandId from base_category_brand " +
				"where categoryId = :categoryId) and status = :status order by sortOrder asc";
		SQLQuery query = baseDao.getSession().createSQLQuery(sql);
		query.addScalar("id", StandardBasicTypes.LONG)
			.addScalar("name")
			.addScalar("pinyin")
			.addScalar("companyId")
			.addScalar("logoPath")
			.addScalar("intro")
			.addScalar("sortOrder",StandardBasicTypes.LONG)
			.setResultTransformer(Transformers.aliasToBean(Brand.class));
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		params.put("status", RecordStatus.normal.toString());
		return baseDao.queryNative(sql, query, params, null);
	}

}
